/*
* Activity Tracking Log v0.1.1 - http://activitytrackinglog.codeplex.com
* Activity Tracking Log for ASP.Net & ASP.Net MVC
* Copyright (c) 2011 Vadim Burac
* Licensed under the Microsoft Public License (Ms-PL)
* http://activitytrackinglog.codeplex.com/license
*/

using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using ActivityTrackingLog.Data.DTOs;
using ActivityTrackingLog.Utils;

namespace ActivityTrackingLog.Data
{
    //[Serializable]
    //public abstract class BaseLinqToSqlEntity
    //{
    //    public T Detach<T>() where T : class
    //    {
    //        T copiedEntity;
    //        BinaryFormatter bf = new BinaryFormatter();
    //        using (MemoryStream ms = new MemoryStream())
    //        {
    //            bf.Serialize(ms, this);
    //            ms.Position = 0;
    //            copiedEntity = (T)bf.Deserialize(ms);
    //        }

    //        return copiedEntity;
    //    }
    //}

    [Serializable]
    partial class ActivityItem// : BaseLinqToSqlEntity
    {
        #region ctors

        public ActivityItem(string userKey,
            string clientHost,
            string applicationKey,
            ActivityTypeEnum activityType,
            string category,
            string activity,
            bool isPostAction = false,
            string activityCode = null,
            string pageUrl = null,
            string queryString = null)
        {
            this.ActivityLogId = Guid.NewGuid();
            this.UserKey = userKey.ToLowerTrimSafe();
            this.ClientHost = clientHost.ToLowerTrimSafe();
            this.ApplicationKey = applicationKey;
            this.ActivityType = activityType.ToString();
            this.Category = category;
            this.Activity = activity;
            this.IsPostAction = Convert.ToByte(isPostAction);
            this.ActivityCode = activityCode;
            this.PageUrl = pageUrl.ToLowerTrimSafe();
            this.QueryString = queryString.ToLowerTrimSafe();
            this.TimeUtc = DateTime.UtcNow;
        }

        #endregion

        public ActivityItemDTO GetDTO()
        {
            return new ActivityItemDTO
            {
                Sequence = this.Sequence,
                ActivityLogId = this.ActivityLogId,
                ApplicationKey = this.ApplicationKey,
                ActivityType = this.ActivityType,
                Category = this.Category,
                ActivityCode = this.ActivityCode,
                Activity = this.Activity,
                IsPostAction = Convert.ToBoolean(this.IsPostAction),
                UserKey = this.UserKey,
                ClientHost = this.ClientHost,
                PageUrl = this.PageUrl,
                QueryString = this.QueryString,
                UserAgentId = this.UserAgentId,
                TimeUtc = this.TimeUtc
            };
        }
    }

    [Serializable]
    partial class UserAgentItem //: BaseLinqToSqlEntity
    {
        public bool IsNew { get; set; }

        public string FullHash
        {
            get
            {
                return string.Format("{0},{1}", this.Hash, this.ApplicationKey);
            }
        }
    }
}
